'use strict';

const { Controller } = require('egg');

class nnController extends Controller {

  // GET请求

  // 异步返回
  async userInfo() {
    const { ctx } = this;
    await new Promise(resolve => {
      setTimeout(() => {
        resolve(ctx.body = 'xx');
      }, 5000);
    });
  }
  /**
   * 自由传参模式
   * ctx.query.name
   */
  async userInfo2() {
    const { ctx } = this;
    ctx.body = `姓名: ${ctx.query.name || '默认值'}`;

  }
  /**
   * 严格传参模式
   * 路由: /grild/:name/:age
   */
  async userInfo3() {
    const { ctx } = this;
    ctx.body = `小红 ${ctx.params.name} 年龄 ${ctx.params.age}`;

  }

  // POST请求

  async addUser() {
    const { ctx } = this;
    ctx.body = {
      status: 200,
      data: ctx.request.body,
    };
  }

  // 通过 service 返回数据
  /**
   *
   * 路由: /getInfo/:id
   */
  async getServiceInfo() {
    const { ctx } = this;
    const id = ctx.params.id;
    ctx.body = id;
  }
  async newContext() {
    const { ctx } = this;
    const params = ctx.params('name');
    console.log(params);
    ctx.body = params;
  }
  async newResponse() {
    const { ctx } = this;
    ctx.response.token = 'jss';
    const vs = ctx.helper.base64Encode('wowow');
    ctx.body = vs;
  }
  async getToken() {
    const { ctx } = this;

    console.log(ctx.request.header.token);
    ctx.body = {
      code: 200,
      data: ctx.request.header.token,
    };
  }
}

module.exports = nnController;
